package com.hudongwx.godoffood.activity.Shopping_Activity;


import android.content.Intent;


import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.hudongwx.godoffood.R;
import com.hudongwx.godoffood.utils.DBUtils.Address;
import com.hudongwx.godoffood.utils.DBUtils.DbOpenUtils;

import org.xutils.DbManager;
import org.xutils.common.util.KeyValue;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_number)
public class NumberActivity extends AppCompatActivity implements IonSlidingViewClickListener {
    @ViewInject(R.id.number_recyclerView)
    RecyclerView recycleView;
    String TAG = "NumberActivity";
    DbManager dbManager;
    Adapter adapter;
    List<Address> all = null;
    List<Address> date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        date = date();
        //判断是否有数据
        if (date != null) {
            //加载布局
            intenView();
        }
    }
//找出控件
    private void intenView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        date = date();
        adapter = new Adapter(date);
        recycleView.setAdapter(adapter);
        recycleView.setItemAnimator(new DefaultItemAnimator());
    }

    /**
     * 在数据库中读取数据
     * @return
     */
    private List<Address> date() {
        dbManager = DbOpenUtils.getShoppingDb2();
        try {
            all = dbManager.findAll(Address.class);

        } catch (DbException e) {
            e.printStackTrace();
        }
        return all;
    }
     //结束页面
    @Event(R.id.number_img_1)
    private void onclik(View view) {
        finish();
    }
    //在activity销毁的时候结束
    @Override
    protected void onDestroy() {
        finish();
        super.onDestroy();
    }
      //添加数据的按钮
    @Event(R.id.number_button)
    private void number_button(View view) {
        Intent intent = new Intent(NumberActivity.this, AddressActivity.class);
        startActivityForResult(intent, 1);
    }
    //传回数据的方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            intenView();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    //重写的点击
    @Override
    public void onItemClick(View view, int position) {

    }
    //重写的点击
    @Override
    public void onDeleteBtnCilck(View view, int position) {
        //删除数据
        adapter.removeData(position);

    }
//设配器且实现IonSlidingButtonListener点击
    class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> implements SlidingButtonView.IonSlidingButtonListener {


        private IonSlidingViewClickListener mIDeleteBtnClickListener;
        List<Address> Mlist;

        private SlidingButtonView mMenu = null;

        public Adapter(IonSlidingViewClickListener mIDeleteBtnClickListener) {
            this.mIDeleteBtnClickListener = mIDeleteBtnClickListener;
        }

        /**
         *
         */


        public Adapter(List<Address> mlist) {
            Mlist = mlist;

        }


//        for (int i = 0; i < 10; i++) {
//            mDatas.add(i+"");
//        }


        @Override
        public int getItemCount() {

            return Mlist.size();
        }

        @Override
        public void onBindViewHolder(final MyViewHolder holder, final int position) {

            holder.tv1.setText(Mlist.get(position).getName());
            holder.tv2.setText(Mlist.get(position).getCall());
            holder.tv3.setText(Mlist.get(position).getAddress_1() + Mlist.get(position).getAddress_2() + "");
           //在item中的checkbox的选中
            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int id = Mlist.get(position).getId();
                    WhereBuilder whereBuilder = WhereBuilder.b();
                    whereBuilder.and("id", "=", id);
                    WhereBuilder whereBuilder_1 = WhereBuilder.b();
                    try {
                        for (int i = 0; i < all.size(); i++) {
                            //设置在数据库中的数据的boolean全是false
                            whereBuilder_1.and("id", "=", all.get(i).getId());
                            int update = dbManager.update(Address.class, whereBuilder_1, new KeyValue("boolean", false));
                            Log.i(TAG, "onClick: " + update);
                        }
                       //在设置点击的那个数据的boolean位true
                        dbManager.update(Address.class, whereBuilder, new KeyValue("boolean", true));
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    //跳转
                    Intent intent = new Intent(NumberActivity.this, ConfirmActivity.class);

                    setResult(1, intent);
                    finish();
                }
            });

            //设置内容布局的宽为屏幕宽度
            holder.layout_content.getLayoutParams().width = Utils.getScreenWidth(NumberActivity.this);

            holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //判断是否有删除菜单打开
                    if (menuIsOpen()) {

                        closeMenu();//关闭菜单
                    } else {

                        int n = holder.getLayoutPosition();
                        mIDeleteBtnClickListener.onItemClick(v, n);
                    }

                }
            });
            //删除的按钮
            holder.btn_Delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int n = holder.getLayoutPosition();

                    int id = Mlist.get(position).getId();
                    try {
                        dbManager.delete(Address.class, WhereBuilder.b("id", "=", id));
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                    removeData(position);
                }
            });
            //编辑的按钮
            holder.btn_bianji.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: ");
                    int n = holder.getLayoutPosition();
                    List<Address> date = new ArrayList<>();
                    Address strings = Mlist.get(position);
                    date.add(strings);
                    if (date.size() != 0) {
                        date.clear();
                        Intent intent = new Intent(NumberActivity.this, AddressActivity.class);
                        intent.putExtra("name", strings.getName());
                        intent.putExtra("address", strings.getAddress_2());
                        intent.putExtra("call", strings.getCall());
                        intent.putExtra("address_1", strings.getAddress_1());
                        intent.putExtra("zip", strings.getZip());
                        startActivity(intent);
                    }
                }
            });
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {

            View view = LayoutInflater.from(NumberActivity.this).inflate(R.layout.layout_item, arg0, false);
            MyViewHolder holder = new MyViewHolder(view);

            return holder;
        }

        @Override
        public void onMenuIsOpen(View view) {
            mMenu = (SlidingButtonView) view;
        }

        /**
         * 滑动或者点击了Item监听
         *
         * @param slidingButtonView
         */
        @Override
        public void onDownOrMove(SlidingButtonView slidingButtonView) {
            if (menuIsOpen()) {
                if (mMenu != slidingButtonView) {
                    closeMenu();
                }
            }
        }


        class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView btn_Delete, btn_bianji;
            RelativeLayout relativeLayout;
            public ViewGroup layout_content;
            TextView tv1, tv2, tv3;
            CheckBox checkBox;

            public MyViewHolder(View itemView) {
                super(itemView);
                btn_Delete = (TextView) itemView.findViewById(R.id.tv_delete);
                relativeLayout = (RelativeLayout) itemView.findViewById(R.id.text);
                layout_content = (ViewGroup) itemView.findViewById(R.id.layout_content);
                btn_bianji = (TextView) itemView.findViewById(R.id.tv_bianji);
                ((SlidingButtonView) itemView).setSlidingButtonListener(Adapter.this);
                tv1 = (TextView) itemView.findViewById(R.id.number_holder_tv_1);
                tv2 = (TextView) itemView.findViewById(R.id.number_holder_tv_2);
                tv3 = (TextView) itemView.findViewById(R.id.number_holder_tv_3);
                checkBox = (CheckBox) itemView.findViewById(R.id.number_holder_checkbox);
            }
        }

        public void addData(int position) {

            Log.i("在这", "addData: ");
            notifyItemInserted(position);
        }

        public void removeData(int position) {
            Log.i("在这", "removeData: ");
            all.remove(position);
            notifyItemRemoved(position);

        }

        /**
         * 删除菜单打开信息接收
         */


        /**
         * 关闭菜单
         */
        public void closeMenu() {
            mMenu.closeMenu();
            mMenu = null;

        }

        /**
         * 判断是否有菜单打开
         */
        public Boolean menuIsOpen() {
            if (mMenu != null) {
                return true;
            }
            Log.i("asd", "mMenu为null");
            return false;
        }


    }


}
